/*
 *  $Id: SoulTrap.cs 937 2009-12-03 12:05:03Z thenn.erannor $
 *	This file is a part of Ragima CCG.
 *	(C) 2008-2009 Ragima development group.
 *
 *	Licensed under the terms of the GNU GPL License version 2.
 *
 */

namespace Ragima.Cards001.Fire.Abilities {
    public class SoulTrap: AbilityCard<Creature> {
        public SoulTrap(): base("001_f_a_soul_trap") {
            Level = CompleteLevel.ReadyForTest;
            Version = 1;
            Element = ElementType.Fire;
            Target = CreatureTarget.Both;
            TapCreatureAfterUse = false;
        }

        protected override void DoAction(TargetArgs<Creature> args) {
            args.Target.AddAffect(new SoulTrapAffect(args.Caster));
        }

        private class SoulTrapAffect: SpellAffect<SoulTrap> {
            private AbstractObject _caster;

            public SoulTrapAffect(AbstractObject caster) {
                _caster = caster;
                Add(new TimeAffect(4));
            }

            public override Affect Clone() {
                return new SoulTrapAffect(_caster);
            }

            public override void SetAffect() {
                base.SetAffect();
                LocalSummary.OnDeath += DoDeath;
            }
            public override void ResetAffect() {
                base.ResetAffect();
                LocalSummary.OnDeath -= DoDeath;
            }

            private void DoDeath(AbstractObject obj) {
                SoulTrapBonus trap = _caster.GetAffect<SoulTrapBonus>();
                if (trap == null)
                    _caster.AddAffect<SoulTrapBonus>();
                else
                    trap.IncreaseStack();
            }
        }

        public class SoulTrapBonus: SpellAffect<SoulTrap> {
            private int _value;

            public SoulTrapBonus(): base(true) {
                DefaultDescription = true;
                _value = 2;
            }

            public override bool Dispellable {
                get { return false; }
            }

            public override string DescriptionText {
                get { return Description.GetText(_value); }
            }

            public int Value {
                get { return _value; }
            }

            public override void SetAffect() {
                base.SetAffect();
                LocalSummary.ChangeMaxHealth(_value);
            }
            public override void ResetAffect() {
                base.ResetAffect();
                LocalSummary.ChangeMaxHealth(-_value);
            }

            public void IncreaseStack() {
                ResetAffect();
                _value += 2;
                SetAffect();
                UpdateDescription();
            }
        }
    }
}
